from django.shortcuts import render,HttpResponse,redirect
import pymysql

# Create your views here.
def index(request):

    return HttpResponse('ok')

def register(request):
    if request.method=='GET':
        return render(request,'register.html')
    else:
        uname=request.POST.get('uname','')
        upwd=request.POST.get('upwd','')
        upwd2=request.POST.get('upwd2','')
        role =request.POST.get('role')

        if upwd==upwd2:

            sql="""select * from user where uname='{}'""".format(uname)
            con = pymysql.connect(user='root', password='123456', database='ai2110a')
            cursor = con.cursor()
            cursor.execute(sql)
            res=cursor.fetchone()
            print(res)
            if res:
                return HttpResponse('用户名重复，请重新注册')
            else:
                #向数据库添加
                sql="""insert into user (uname,upwd,role) values ('{}','{}','{}')""".format(uname,upwd,role)
                cursor.execute(sql)

            cursor.close()
            con.commit()
            con.close()

            return redirect('leave:login')
            #查询数据库 看uname有没有用户
        #   如果没有 再把用户名和密码保存到数据库

        else:
            return HttpResponse('两次密码不一致')


def login(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    else:
        uname = request.POST.get('uname', '')
        upwd = request.POST.get('upwd', '')

        sql = """select * from user where uname='{}'and upwd='{}'""".format(uname,upwd)
        con = pymysql.connect(user='root', password='123456', database='ai2110a')
        cursor = con.cursor()
        cursor.execute(sql)
        res = cursor.fetchone()
        cursor.close()
        con.close()
        if res:
            role=res[3]
            if role == 1:
                request.session['uname']=uname
                request.session['role']=1


                return redirect('leave:show_leaves')
            elif role ==2:
                request.session['uname'] = uname
                request.session['role'] = 2
                return redirect('leave:add_leave')
            else:
                return HttpResponse('禁止登录')
        else:
            return HttpResponse('登录失败')

def add_leave(request):
    uname = request.session.get('uname', '')
    role = request.session.get('role', '')
    if not uname:
        return redirect('leave:login')
    else:
        if role == 2:
            pass
        elif role == 1:
            return HttpResponse('没有权限')

    if request.method=='GET':
        sql = """select * from leave_type"""
        con = pymysql.connect(user='root', password='123456', database='ai2110a')
        cursor = con.cursor()
        cursor.execute(sql)
        types = cursor.fetchall()
        cursor.close()
        con.close()
        return render(request,'add_leave.html',{'types':types})
    if request.method=='POST':
        stu_name=request.POST.get('stu_name','')
        context=request.POST.get('context','')
        createTime=request.POST.get('createTime','')
        tid=request.POST.get('tid','')
        sql = """insert into leaver (stu_name,createTime,context,tid) values ('{}','{}','{}','{}')""".format(stu_name,createTime,context,tid)
        con = pymysql.connect(user='root', password='123456', database='ai2110a')
        cursor = con.cursor()
        cursor.execute(sql)
        types = cursor.fetchall()
        cursor.close()
        con.commit()
        con.close()

        return HttpResponse("添加成功")


def show_leaves(request):
    uname=request.session.get('uname','')
    role=request.session.get('role','')
    if not uname:
        return redirect('leave:login')
    else:
        if role == 1:
            pass
        elif role == 2:
            return HttpResponse('没有权限')

    page=request.GET.get('page','1')
    page=int(page)
    status=request.GET.get('status','-1')
    status=int(status)

    sql1="""select * from leave_type"""
    con = pymysql.connect(user='root', password='123456', database='ai2110a')
    cursor = con.cursor()
    cursor.execute(sql1)
    leave_types = cursor.fetchall()
    leave_types_dict = dict(leave_types)
    if status == -1:
        sql="""select * from leaver"""
    else:
        sql="""select * from leaver where tid='{}'""".format(status)
    cursor.execute(sql)
    leaves = cursor.fetchall()
    leaves_res=[]
    for leave in leaves:
        list_temp=list(leave)
        list_temp[5]=leave_types_dict[list_temp[5]]
        leaves_res.append(list_temp)
    pagesize=2
    le=len(leaves_res)
    max_y=le % pagesize
    max_z=le // pagesize
    if max_y != 0:
        max_z+=1

    leaves_res=leaves_res[2 * (page-1):2 * (page)]
    print(leaves_res)
    cursor.close()
    con.close()
    return render(request, 'show_leaves.html',{'leaves_res':leaves_res,'leave_types':leave_types,'status':status,'page':page,'max_z':max_z})


def update_leave(request):
    id=request.GET.get('id')
    status=request.GET.get('status')
    id=int(id)
    status=int(status)

    sql = """update leaver set status='{}' where id='{}'""".format(status,id)
    con = pymysql.connect(user='root', password='123456', database='ai2110a')
    cursor = con.cursor()
    cursor.execute(sql)
    con.commit()
    cursor.close()
    con.close()
    return redirect("leave:show_leaves")












